package com.iptv.grape.event.imp;

import com.google.gson.Gson;
import com.iptv.grape.BaseActivity;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.domain.AssetPlayData;
import com.iptv.grape.event.EventArgs;
import com.iptv.grape.event.JSRequest;
import com.iptv.grape.event.data.VodData;
import com.iptv.grape.event.msg.EsObj;
import com.iptv.grape.event.msg.SubAsset;
import com.iptv.grape.event.msg.VodWindowDataArgs;
import com.iptv.grape.event.proc.IWebViewMessageProc;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.CommUtil;
import com.iptv.grape.video.VideoSize;
import com.iptv.grape.video.XMediaPlayer;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

// 初始化点播小窗口
public class VodWindow implements IWebViewMessageProc {
    private VodWindowDataArgs argsData;

    @Override
    public void init(JSRequest jsRequest) {
        argsData = new Gson().fromJson(jsRequest.getJson(), VodWindowDataArgs.class);
    }

    @Override
    public String procMessage(BaseActivity baseActivity, XMediaPlayer mediaPlayer) {
        if (argsData != null) {
            // 发送初始化点播小窗口事件

            VideoSize videoSize = CommUtil.getVideoSize(baseActivity, argsData);


//            AssetPlayData assetPlayData = GrapeContext.curGrapeContext().getAssetPlayDataList().find(argsData.getData());
//            List<SubAsset> subList = argsData.getSubAssetList();
//            assetPlayData.setSubList(subList);
//            assetPlayData.setCurIndex(argsData.getCurIndex());

            AssetPlayData assetPlayData = null;
            if (mediaPlayer.isPlaying() && GrapeContext.curGrapeContext().getCurPlayType().equals("Vod")) {
                // 如正在播放点播
                mediaPlayer.stop();
                // 如果播放的是同一个剧集
                AssetPlayData curAssetPlayData = GrapeContext.curGrapeContext().getCurPlayVod();
                if (curAssetPlayData != null && curAssetPlayData.getAssetID().equals(argsData.getData().getAssetID()) &&
                        curAssetPlayData.getCurIndex() == argsData.getCurIndex()) {
                    // 从头播放当前剧集 设置进度为0
                    //AppLog.getCurLog().out("重新播放");
                    curAssetPlayData.setCurLastPlayTime(0);
                    assetPlayData = curAssetPlayData;
                } else {
                    assetPlayData = buildeAssetPlayData();
                }
            } else {
                assetPlayData = buildeAssetPlayData();
            }

            mediaPlayer.seekToVod(assetPlayData.getCurLastPlayTime());
            mediaPlayer.play(baseActivity, assetPlayData.getPlayUrl(), videoSize);
            GrapeContext.curGrapeContext().setPlaying(true);
            GrapeContext.curGrapeContext().setCurPlayType("Vod");
            VodData.create(argsData);
            //EventBus.getDefault().post(new EventArgs(AppConstant.Vod, AppConstant.DataAcquisition, vodData));
        }
        return "";
    }

    private AssetPlayData buildeAssetPlayData() {
        AssetPlayData assetPlayData = GrapeContext.curGrapeContext().getAssetPlayDataList().find(argsData.getData());
        List<EsObj> esList = argsData.getSubAssetList();



        assetPlayData.setSubList(esList);
        assetPlayData.setCurIndex(argsData.getCurIndex());
        GrapeContext.curGrapeContext().setCurPlayVod(assetPlayData);
        return assetPlayData;
    }
}
